package com.sihai.ehr.jdbc;

import com.sihai.ehr.model.biz.AttendanceNew;
import com.sihai.ehr.model.biz.EmployeeinfoBase;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import org.apache.commons.lang.xwork.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * author: wxn
 * since: 13-9-8 下午10:39
 * 考勤服务
 */
public class KaoQinService {
    private Connection connection;

    public void connection() throws Exception {
        connection = JDBCHelper.connection();
        if (connection == null) {
            throw new IllegalArgumentException("jdbc连接失败");
        }
    }

    public void close() throws Exception {
        if (connection != null && !connection.isClosed()) {
            JDBCHelper.close(connection);
        }
    }

    public List<AttendanceNew> findByYearAndMonth(List<AttendanceNew> oldAttendances, List<EmployeeinfoWork> works, int year, int month) {
        String yuefen = String.format("%s-%s", year, month < 10 ? "0"+month : month);
        System.out.println("wxn---yuefen="+yuefen);
        System.out.println("读考勤月报表start***********************");
        try{
            PreparedStatement pst = connection.prepareStatement("select * from zt_mon where yuefen = ?");
            pst.setString(1, yuefen);
            ResultSet rs = pst.executeQuery();
            List<AttendanceNew> attendances = new ArrayList<AttendanceNew>();
            while (rs.next()) {
                try {
                    long userId = rs.getLong("user_serial");
                    AttendanceNew attendance = findAttendanceNewByzBaseId(oldAttendances, userId); //查询老的对象
                    if(attendance == null){
                        attendance = new AttendanceNew();
                    }

                    attendance.setAttendYear(year);
                    attendance.setAttendMonth(month);
                    EmployeeinfoWork work = getEmployeeinfoWork(works, userId);
                    if(work == null){
                        continue;
                    }
                    attendance.setEmployeeinfoWork(work);

                    attendance.setAllDays(rs.getInt("ts")); //汇总天数
                    attendance.setShouldDays(rs.getFloat("yingchu1")); //应出天数
                    attendance.setShouldHours(rs.getFloat("yingchu2")); //应出时间
                    attendance.setShouldTimes(rs.getInt("yingchu3")); //应出次数
                    attendance.setRealDays(rs.getFloat("shichu1")); //应出天数
                    attendance.setRealHours(rs.getFloat("shichu2")); //应出时间
                    attendance.setRealTimes(rs.getInt("shichu3")); //应出次数
                    attendance.setAwayDays(rs.getFloat("kugong")); //旷工天数
                    attendance.setAwayTimes(rs.getInt("kugongcs")); //旷工次数
                    attendance.setLateDays(rs.getFloat("cdshi")); //迟到天数
                    attendance.setLateTimes(rs.getInt("cdci")); //迟到次数
                    attendance.setLeaveEarlyDays(rs.getFloat("ztshi")); //早退天数
                    attendance.setLeaveEarlyTimes(rs.getInt("ztci")); //早退次数
                    attendance.setYqdTimes(rs.getInt("yqd")); //应签到次数
                    attendance.setYqtTimes(rs.getInt("yqt")); //应签退次数
                    attendance.setWqdTimes(rs.getInt("wqd")); //未签退次数
                    attendance.setWqtTimes(rs.getInt("wqt")); //未签退次数
                    attendance.setRestDays(rs.getFloat("xxr")); //休息日数
                    attendance.setHolidayDays(rs.getFloat("jjr")); //节假日数
                    attendance.setzOvertimeDays(rs.getFloat("jbzao")); //早加班日数
                    attendance.setzOvertimeTimes(rs.getInt("jbzaocs")); //早加班次数
                    attendance.setwOvertimeDays(rs.getFloat("jbwan")); //晚加班日数
                    attendance.setwOvertimeTimes(rs.getInt("jbwancs")); //晚加班次数
                    attendance.setPsOvertimeTimes(rs.getFloat("jbzong")); //平时实际加班时间
                    attendance.setZrOvertimeDays(rs.getFloat("jbzhou")); //周日加班日数
                    attendance.setZrOvertimeTimes(rs.getInt("jbzhoucs")); //周日加班次数
                    attendance.setJrOvertimeDays(rs.getFloat("jbjie")); //节日加班日数
                    attendance.setJrOvertimeTimes(rs.getInt("jbjiecs")); //节日加班次数
                    attendance.setQjDays(rs.getFloat("qjsj")); //请假日数
                    attendance.setQjTimes(rs.getInt("qjcs")); //请假次数

                    attendances.add(attendance);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            return attendances;

        }catch(SQLException e) {
            e.printStackTrace();
        }
        System.out.println("读考勤月报表end***********************");
        return null;
    }

    private EmployeeinfoWork getEmployeeinfoWork(List<EmployeeinfoWork> works, long baseId){
        if(works == null || works.isEmpty()){
            return null;
        }
        for(EmployeeinfoWork work : works){
            if(work.getBaseId() != null && work.getBaseId().getId() != null && work.getBaseId().getId().longValue() == baseId){
                return work;
            }
        }
        return null;
    }

    private AttendanceNew findAttendanceNewByzBaseId(List<AttendanceNew> attendances, long baseId){
        if(attendances == null || attendances.isEmpty() || baseId <= 0){
            return null;
        }
        for(AttendanceNew a : attendances){
            EmployeeinfoBase base = a.getEmployeeinfoWork().getBaseId();
            if(base != null && base.getId() != null && base.getId().longValue() == baseId){
                return a;
            }
        }
        return null;
    }
}
